package com.libre.qr;

import com.google.zxing.*;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.geom.RoundRectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;

@Slf4j
public class QrCodeUtils {

    private static final String CHARSET = "utf-8";
    private static final String FORMAT_NAME = "JPG";

    /**
     * 二维码的尺寸
     */
    private static final int QRCODE_SIZE = 300;

    /**
     *     logo的宽和高
     */
    private static final int WIDTH = 60;
    private static final int HEIGHT = 60;

    /**
     * 生成二维码图片
     *
     * @param content      二维码内容
     * @param imagePath    二维码内部图片路径
     * @param needCompress 二维码内部图片是否需要压缩标识
     * @return 二维码图片
     * @throws WriterException 异常
     */
    private static BufferedImage createQrCode(String content,String imagePath,boolean needCompress) throws WriterException, IOException {
        /**
         * EncodeHintType 编码提示类型 枚举类型:
         * CHARACTER_SET: 设置字符编码类型 "UTF-8" QR_CODE类型默认的编码格式 "ISO-8859-1"
         * MARGIN: 设置二维码边距，单位像素，值越小，二维码距离四周越近
         * ERROR_CORRECTION: 设置误差校正
         *  ErrorCorrectionLevel：误差校正等级，L = ~7% correction、M = ~15% correction、Q = ~25% correction、H = ~30% correction
         *    不设置时，默认为 L 等级，等级不一样，生成的图案不同，但扫描的结果是一样的
         */
        Map<EncodeHintType, Object> hints = new ConcurrentHashMap<>(3);
        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
        hints.put(EncodeHintType.CHARACTER_SET, CHARSET);
        hints.put(EncodeHintType.MARGIN, 1);
        //加密生成二维码的矩阵对象 二维的0 1 然后根据特定的值选择属性
        BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, QRCODE_SIZE, QRCODE_SIZE, hints);
        int width = bitMatrix.getWidth();
        int height = bitMatrix.getHeight();
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        for (int i = 0; i < width; i++) {
            for (int j = 0; j < height; j++) {
                //设置二维码的黑色0xFF000000 和白色0xFFFFFFFF
                image.setRGB(i, j, bitMatrix.get(i, j) ? 0xFF000000 : 0xFFFFFFFF);
            }
        }
        if (StringUtils.isEmpty(imagePath)) {
            return image;
        }
        //插入小图标 logo
        QrCodeUtils.insertImage(image, imagePath, needCompress);
        return image;
    }

    /**
     * 创建线性的条形码
     *
     * @param content 条形码内容
     * @return 生成的条形码
     * @throws WriterException 异常
     */
    private static BufferedImage createLinearCode(String content) throws WriterException {
        Map<EncodeHintType, Object> hints = new ConcurrentHashMap<>();
        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
        hints.put(EncodeHintType.CHARACTER_SET, CHARSET);
        hints.put(EncodeHintType.MARGIN, 1);
        BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.ITF, QRCODE_SIZE, 200, hints);
        int width = bitMatrix.getWidth();
        int height = bitMatrix.getHeight();
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        for (int i = 0; i < width; i++) {
            for (int j = 0; j < height; j++) {
                image.setRGB(i, j, bitMatrix.get(i, j) ? 0xFF000000 : 0xFFFFFFFF);
            }
        }
        return image;
    }

    /**
     * 将logo插入二维码中间
     *
     * @param source       生成的二位码存储对象
     * @param imagePath    二维码内部图片的路径
     * 一般的业务场景会把插入的小logo放到oss生成url
     * @param needCompress 二维码内部图片是否需要压缩
     * @throws IOException 异常
     */
    private static void insertImage(BufferedImage source, String imagePath, boolean needCompress) throws IOException {
        File file = new File(imagePath);
        if (!file.exists()) {
            //可以自定义抛出异常
            System.out.println("文件路径不存在:" + imagePath);
            return;
        }
        //这里修改ImageIO.read() 内可以接受Url InputStream File ImageInputStream
        Image src  = ImageIO.read(file);
        int width = src.getWidth(null);
        int height = src.getHeight(null);
        //是否压缩
        if (needCompress) {
            if (width > WIDTH) {
                width = WIDTH;
            }
            if (height > HEIGHT) {
                height = HEIGHT;
            }
            Image image = src.getScaledInstance(width, height, Image.SCALE_SMOOTH);
            BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
            Graphics g = tag.getGraphics();
            g.drawImage(image, 0, 0, null);
            g.dispose();
            src = image;
        }
        Graphics2D graphics = source.createGraphics();
        int x = (QRCODE_SIZE - width) / 2;
        int y = (QRCODE_SIZE - height) / 2;
        graphics.drawImage(src,x,y,width,height,null);
        Shape shape = new RoundRectangle2D.Float(x, y, width, width, 6, 6);
        graphics.setStroke(new BasicStroke(3f));
        graphics.draw(shape);
        graphics.dispose();
    }

    /**
     * 生成带logo二维码，并保存到磁盘
     *
     * @param content      二维码的内容
     * @param imgPath      二维码内部插入的图片路径
     * @param destPath     整个二维码存储的路径
     * @param needCompress 二维码内部插入的图片是否需要压缩的标识
     * @throws Exception 异常
     */
    public static void encode(String content, String imgPath, String destPath, boolean needCompress) throws Exception {
        BufferedImage image = QrCodeUtils.createQrCode(content, imgPath, needCompress);
        mkdirs(destPath);
        //生成随机文件名
        String file = UUID.randomUUID() + ".jpg";
        ImageIO.write(image, FORMAT_NAME, new File(destPath + "/" + file));
    }

    /**
     * 生成条形码
     *
     * @param content  内容 不能有中文
     * @param destPath 文件存放路径
     * @throws Exception
     */
    public static void encode1(String content,String destPath) throws Exception {
        BufferedImage image = QrCodeUtils.createLinearCode(content);
        mkdirs(destPath);
        //生成随机文件名
        String file = UUID.randomUUID() + ".jpg";
        ImageIO.write(image, FORMAT_NAME, new File(destPath + "/" + file));
    }

    public static void mkdirs(String destPath) {
        File file = new File(destPath);
        // 当文件夹不存在时，mkdirs会自动创建多层目录，区别于mkdir。(mkdir如果父目录不存在则会抛出异常)
        if (!file.exists() && !file.isDirectory()) {
            file.mkdirs();
        }
    }

    public static void encode(String content, String imgPath, OutputStream output, boolean needCompress)
            throws Exception {
        BufferedImage image = QrCodeUtils.createQrCode(content, imgPath, needCompress);
        ImageIO.write(image, FORMAT_NAME, output);
    }

    //我主要用的是这个方法，其他方法我没有用
    public static void encode(String content, OutputStream output) throws Exception {
        QrCodeUtils.encode(content, null, output, false);
    }








    /**
     * 条形码编码
     *
     * @param contents
     * @param width
     * @param height
     * @param imgPath
     */
    public static void encode(String contents, int width, int height, String imgPath) {
        int codeWidth = 3 + // start guard
                (7 * 6) + // left bars
                5 + // middle guard
                (7 * 6) + // right bars
                3; // end guard
        codeWidth = Math.max(codeWidth, width);
        try {
            BitMatrix bitMatrix = new MultiFormatWriter().encode(contents,BarcodeFormat.EAN_13, codeWidth, height, null);
            MatrixToImageWriter.writeToFile(bitMatrix, "png", new File(imgPath));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 条形码解码
     *
     * @param imgPath
     * @return String
     */
    public static String decode(String imgPath) {
        BufferedImage image = null;
        Result result = null;
        try {
            image = ImageIO.read(new File(imgPath));
            if (image == null) {
                System.out.println("the decode image may be not exit.");
            }
            LuminanceSource source = new BufferedImageLuminanceSource(image);
            BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
            result = new MultiFormatReader().decode(bitmap, null);
            return result.getText();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    private static String path = System.getProperty("user.dir") + "\\src\\main\\resources\\qr\\";
    public static void main(String[] args) throws Exception {
        // encode1( "23542512",path);


        String imgPath = path+"/zxing_EAN-13.png";
        String contents = "6926557300360";
        int width = 105, height = 50;
        encode(contents, width, height, imgPath);
        System.out.println("finished zxing EAN-13 encode.");
        String decodeContent = decode(imgPath);
        System.out.println("解码内容如下：" + decodeContent);
        System.out.println("finished zxing EAN-13 decode.");
    }


}
